package com.wait.onlinepicturecloud.application.service.impl;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wait.onlinepicturecloud.infrastructure.manage.upload.file.UploadPictureResult;
import com.wait.onlinepicturecloud.domain.user.service.UserDomainService;
import com.wait.onlinepicturecloud.infrastructure.common.DeleteRequest;
import com.wait.onlinepicturecloud.infrastructure.exception.BusinessException;
import com.wait.onlinepicturecloud.infrastructure.exception.ErrorCode;
import com.wait.onlinepicturecloud.infrastructure.exception.ThrowUtils;
import com.wait.onlinepicturecloud.infrastructure.mapper.UserMapper;
import com.wait.onlinepicturecloud.interfaces.dto.user.UserLoginRequest;
import com.wait.onlinepicturecloud.interfaces.dto.user.UserQueryRequest;
import com.wait.onlinepicturecloud.interfaces.dto.user.UserRegisterRequest;
import com.wait.onlinepicturecloud.interfaces.vo.user.LoginUserVO;
import com.wait.onlinepicturecloud.interfaces.vo.user.UserVO;
import com.wait.onlinepicturecloud.application.service.UserApplicationService;
import com.wait.onlinepicturecloud.domain.user.entity.User;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.List;
import java.util.Set;

/**
 * @author Wait葡萄成熟时
 * @description 针对表【user(用户)】的数据库操作Service实现
 * @createDate 2025-03-09 11:44:33
 */
@Service
@Slf4j
public class UserApplicationServiceImpl implements UserApplicationService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private UserDomainService userDomainService;

    /**
     * description 用户注册
     * author Wait葡萄成熟时
     */
    @Override
    public Long userRegister(UserRegisterRequest userRegisterRequest) {
        // 1. 校验
        User.vailUserRegister(userRegisterRequest);
        // 2.执行
        return userDomainService.userRegister(userRegisterRequest);

    }

    /**
     * description 用户登录
     * author Wait葡萄成熟时
     */
    @Override
    public LoginUserVO userLogin(UserLoginRequest userLoginRequest, HttpServletRequest request) {
        User.validUserLogin(userLoginRequest);

        return userDomainService.userLogin(userLoginRequest, request);
    }

    /**
     * description 从请求头获取当前登录用户
     * author Wait葡萄成熟时
     */
    @Override
    public User getLoginUser(HttpServletRequest request) {
        return userDomainService.getLoginUser(request);
    }

    /**
     * description 用户登录注销
     * author Wait葡萄成熟时
     */
    @Override
    public boolean userLogout(HttpServletRequest request) {
return userDomainService.userLogout(request);
    }

    /**
     * description 获取脱敏后的单个用户信息（User转为UserVO）
     * author Wait葡萄成熟时
     */
    @Override
    public UserVO getUserVO(User user) {
return userDomainService.getUserVO(user);
    }

    /**
     * description 获取脱敏后的用户列表（User转为UserVO）
     * author Wait葡萄成熟时
     */
    @Override
    public List<UserVO> getUserVOList(List<User> userList) {
return userDomainService.getUserVOList(userList);
    }

    /**
     * description 用于将查询请求转为 QueryWrapper 对象的方法
     * author Wait葡萄成熟时
     */
    @Override
    public QueryWrapper<User> getQueryWrapper(UserQueryRequest userQueryRequest) {
        if (userQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }
        Long id = userQueryRequest.getId();
        String userAccount = userQueryRequest.getUserAccount();
        String userName = userQueryRequest.getUserName();
        String userProfile = userQueryRequest.getUserProfile();
        String userRole = userQueryRequest.getUserRole();
        String sortField = userQueryRequest.getSortField();
        String sortOrder = userQueryRequest.getSortOrder();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(ObjUtil.isNotNull(id), "id", id);
        queryWrapper.eq(StrUtil.isNotBlank(userRole), "userRole", userRole);
        queryWrapper.like(StrUtil.isNotBlank(userAccount), "userAccount", userAccount);
        queryWrapper.like(StrUtil.isNotBlank(userName), "userName", userName);
        queryWrapper.like(StrUtil.isNotBlank(userProfile), "userProfile", userProfile);
        queryWrapper.orderBy(StrUtil.isNotEmpty(sortField), sortOrder.equals("ascend"), sortField);
        return queryWrapper;
    }


    /**
     * description 密码加密
     * author Wait葡萄成熟时
     */
    @Override
    public String getEncryptPassword(String userPassword) {
        return userDomainService.getEncryptPassword(userPassword);
    }

    /**
     * description 获得脱敏后的用户信息
     * author Wait葡萄成熟时
     */
    @Override
    public LoginUserVO getLoginUserVO(User user) {
 return userDomainService.getLoginUserVO(user);
    }


    @Override
    public User getUserById(long id) {
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        User user = userDomainService.getById(id);
        ThrowUtils.throwIf(user == null, ErrorCode.NOT_FOUND_ERROR);
        return user;
    }

    @Override
    public UserVO getUserVOById(long id) {
        return userDomainService.getUserVO(getUserById(id));
    }

    @Override
    public boolean deleteUser(DeleteRequest deleteRequest) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return userDomainService.removeById(deleteRequest.getId());
    }

    @Override
    public void updateUser(User user) {
        boolean result = userDomainService.updateById(user);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
    }

    @Override
    public Page<UserVO> listUserVOByPage(UserQueryRequest userQueryRequest) {
        ThrowUtils.throwIf(userQueryRequest == null, ErrorCode.PARAMS_ERROR);
        long current = userQueryRequest.getCurrent();
        long size = userQueryRequest.getPageSize();
        Page<User> userPage = userDomainService.page(new Page<>(current, size),
                userDomainService.getQueryWrapper(userQueryRequest));
        Page<UserVO> userVOPage = new Page<>(current, size, userPage.getTotal());
        List<UserVO> userVO = userDomainService.getUserVOList(userPage.getRecords());
        userVOPage.setRecords(userVO);
        return userVOPage;
    }

    @Override
    public List<User> listByIds(Set<Long> userIdSet) {
        return userDomainService.listByIds(userIdSet);
    }

    @Override
    public Long addUser(User user) {

        return userDomainService.addUser(user);
    }

    @Override
    public UploadPictureResult uploadAvatar(MultipartFile multipartFile) {
        return userDomainService.uploadAvatar(multipartFile);

    }

}




